package com.hxd.otc.module.login.fragment;

import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.hxd.otc.R;
import com.hxd.otc.base.BaseFragment;
import com.hxd.otc.common.RecentLoginAccountInfoHelper;
import com.hxd.otc.dialog.FingerprintIdentifyDialog;
import com.hxd.otc.module.login.activity.LoginActivity;
import com.hxd.otc.mvp.base.BasePresenter;
import com.hxd.otc.utils.UIUtils;
import com.wei.android.lib.fingerprintidentify.base.BaseFingerprint;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by lzy on 2019/1/8 0008.
 * description ${指纹登录}
 */
public class FingerprintLoginFragment extends BaseFragment implements View.OnClickListener {
    @BindView(R.id.iv_fingerprint)
    ImageView mIvFingerprint;
    @BindView(R.id.tv_login)
    TextView mTvLogin;
    @BindView(R.id.tv_switch_login_mode)
    TextView mTvSwitchLoginMode;
    @BindView(R.id.tv_account)
    TextView mTvAccount;

    private FingerprintIdentifyDialog mFingerprintIdentifyDialog;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_fingerprint_login;
    }

    @Override
    protected void initView() {
        mTvAccount.setText(RecentLoginAccountInfoHelper.getAccount());
    }

    @Override
    protected void initData() {
        showFingerprintPasswordDialog();
    }

    @Override
    protected void initEvent() {
    }

    @OnClick({R.id.iv_close, R.id.tv_register, R.id.iv_fingerprint, R.id.tv_login, R.id.tv_switch_login_mode})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_close:
                onBackPressed();
                break;
            case R.id.tv_register:
                clickRegister();
                break;
            case R.id.iv_fingerprint:
            case R.id.tv_login:
                showFingerprintPasswordDialog();
                break;
            case R.id.tv_switch_login_mode:
                onSwitchLoginMode();
                break;
        }
    }

    private void onSwitchLoginMode() {
        FragmentActivity activity = getActivity();
        if (activity != null && activity instanceof LoginActivity) {
            ((LoginActivity) activity).onSwitchLoginMode(LoginActivity.LoginMethod.NOR);
        }
    }

    private void clickRegister() {
        FragmentActivity activity = getActivity();
        if (activity != null && activity instanceof LoginActivity) {
            ((LoginActivity) activity).clickRegister();
        }
    }

    private void onBackPressed() {
        FragmentActivity activity = getActivity();
        if (activity != null && activity instanceof LoginActivity) {
            ((LoginActivity) activity).onBackPressed();
        }
    }

    private void showFingerprintPasswordDialog() {
        if (mFingerprintIdentifyDialog == null) {
            createFingerprintPasswordDialog();
        } else {
            UIUtils.dismissDialog(mFingerprintIdentifyDialog);
        }
        mFingerprintIdentifyDialog.show();
    }

    private void createFingerprintPasswordDialog() {
        mFingerprintIdentifyDialog = new FingerprintIdentifyDialog(getContext())
                .setTitleText(getString(R.string.app_name) + getString(R.string.fingerprint_login))
                .setContentText(getString(R.string.please_verify_your_mobile_phone_fingerprint))
                .setIdentifyListener(new BaseFingerprint.IdentifyListener() {
                    @Override
                    public void onSucceed() {
                        login();
                        UIUtils.dismissDialog(mFingerprintIdentifyDialog);
                    }

                    @Override
                    public void onNotMatch(int i) {
                    }

                    @Override
                    public void onFailed(boolean b) {
                    }

                    @Override
                    public void onStartFailedByDeviceLocked() {
                    }
                });
    }

    private void login() {
        FragmentActivity activity = getActivity();
        if (activity != null && activity instanceof LoginActivity) {
            ((LoginActivity) activity).login(RecentLoginAccountInfoHelper.getAccount(),
                    RecentLoginAccountInfoHelper.getPassword(), true);
        }

    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    public void setClickEnable(boolean enable) {
        mTvLogin.setOnClickListener(enable ? this : null);
        mIvFingerprint.setOnClickListener(enable ? this : null);
    }
}
